package com.xq.tmall.agu.ferign;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xq.tmall.agu.core.agent.WebAgent;
import com.xq.tmall.agu.task.EasyCrawl;
import com.xq.tmall.entity.WebPage;
import com.xq.tmall.util.*;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Callable;

/**
 * 交易日列表爬虫
 * @author chenguoxiang
 * @create 2019-03-26 17:57
 *  http://www.szse.cn/api/report/exchange/onepersistenthour/monthList?random=0.2812774184280482
 **/
@Slf4j
public class TradingDateSpider implements Callable<Map<String,Boolean>>{
    private final String baseUrl="http://www.szse.cn/api/report/exchange/onepersistenthour/monthList";
    private String month;
    private String url;

    public TradingDateSpider(String month){
        this.month=month;
        url=null==month?baseUrl:String.format("%s?month=%s",baseUrl,month);
    }

    @Override
    public Map<String, Boolean> call() {
        IpFerginUtil ipFerginUtil = SpringContextUtil.getBean(IpFerginUtil.class);
        Map<String, Boolean> data = new HashMap<>();
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ipFerginUtil.getRedisIp(), Integer.parseInt(ipFerginUtil.getRedisPort())));
        log.info("TradingDateSpider====proxy,ip:{},port:{}",ipFerginUtil.getRedisIp(),ipFerginUtil.getRedisPort());
        try {
            if(proxy == null){
                log.info("未设置代理请检查!");
                throw new Exception("未设置代理请检查");
            }
            WebPage webPage= JsoupUitl.getWebPage(url, Connection.Method.GET,
                    8000,ipFerginUtil.getUseAgent(),"http://www.szse.cn/disclosure/index.html",ipFerginUtil.getRedisCookies(),proxy,true,null);
            log.info("调用TradingDateSpider==webPage:" + webPage);
            Optional.ofNullable(webPage).map(webPage1 -> webPage1.getDocument().text()).orElseThrow(()->new Exception("web page null"));
            JSONObject json = JSON.parseObject(webPage.getDocument().text());
            JSONArray jsonArray = json.getJSONArray("data");
            for (int i = 0; i < jsonArray.size(); i++) {
                data.put(
                        jsonArray.getJSONObject(i).getString("jyrq"),
                        jsonArray.getJSONObject(i).getInteger("jybz")==1
                );
            }
        }catch (Exception e){
            log.error("page process error {} {}", url, e);
        }
        return data;
    }
}
